package com.example.recruitment.controller;

import com.example.recruitment.entity.Interview;
import com.example.recruitment.entity.Job;
import com.example.recruitment.entity.Resume;
import com.example.recruitment.service.InterviewService;
import com.example.recruitment.service.JobService;
import com.example.recruitment.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/interview")
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    @Autowired
    private JobService jobService;
    
    @Autowired
    private ResumeService resumeService;

    @GetMapping("/list")
    public String list(@RequestParam(required = false) String realName,
                      @RequestParam(required = false) Long jobId,
                      @RequestParam(required = false) String status,
                      Model model) {
        // 创建查询条件对象
        Interview query = new Interview();
        if (realName != null && !realName.isEmpty()) {
            query.setRealName(realName);
        }
        if (jobId != null) {
            query.setJobId(jobId);
        }
        if (status != null && !status.isEmpty()) {
            query.setStatus(status);
        }
        
        // 查询面试列表
        List<Interview> interviews = interviewService.findAll(query);
        List<Job> jobs = jobService.findAll(null); // 获取所有职位
        
        model.addAttribute("interviews", interviews);
        model.addAttribute("jobs", jobs);
        
        // 保存搜索条件
        model.addAttribute("realName", realName);
        model.addAttribute("jobId", jobId);
        model.addAttribute("status", status);
        
        return "interview/list";
    }

    @GetMapping("/resume/{resumeId}")
    public String resumeInterviews(@PathVariable Long resumeId, Model model) {
        List<Interview> interviews = interviewService.findByResumeId(resumeId);
        model.addAttribute("interviews", interviews);
        return "interview/list";
    }

    @GetMapping("/add")
    public String addPage(Model model) {
        List<Resume> resumes = resumeService.findAll();
        model.addAttribute("resumes", resumes);
        return "interview/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public String add(@RequestBody Interview interview) {
        if (interviewService.add(interview)) {
            return "success";
        }
        return "fail";
    }

    @GetMapping("/edit/{id}")
    public String editPage(@PathVariable Long id, Model model) {
        Interview interview = interviewService.findById(id);
        List<Resume> resumes = resumeService.findAll();
        model.addAttribute("interview", interview);
        model.addAttribute("resumes", resumes);
        return "interview/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    public String update(@RequestBody Interview interview) {
        if (interviewService.update(interview)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable Long id) {
        if (interviewService.deleteById(id)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/status/{id}")
    @ResponseBody
    public String updateStatus(@PathVariable Long id, @RequestParam String status) {
        if (interviewService.updateStatus(id, status)) {
            return "success";
        }
        return "fail";
    }

    @PostMapping("/feedback/{id}")
    @ResponseBody
    public String updateFeedback(@PathVariable Long id, @RequestParam String feedback) {
        if (interviewService.updateFeedback(id, feedback)) {
            return "success";
        }
        return "fail";
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Interview getById(@PathVariable Long id) {
        return interviewService.findById(id);
    }
} 